package cn.kkdlk.parse.dxf.transformation.impl;


import cn.kkdlk.parse.dxf.transformation.DxfLineTransformation;
import cn.kkdlk.parse.entities.Arc;
import cn.kkdlk.parse.entities.Circle;
import cn.kkdlk.parse.entities.LW2DVertex;
import cn.kkdlk.parse.entities.LWPolyline;
import cn.kkdlk.parse.math.Point3D;

import java.util.ArrayList;
import java.util.List;

public class DxfLineTransformationImpl implements DxfLineTransformation {

    /**
     * 实例对象
     */
    private static DxfLineTransformation dxfLineTransformation;

    /**
     * 采用单例模式
     *
     * @return DxfLineTransformation
     */
    public static DxfLineTransformation getSingleInstance() {
        if (dxfLineTransformation == null) {
            synchronized (DxfLineTransformation.class) {
                if (dxfLineTransformation == null) {
                    dxfLineTransformation = new DxfLineTransformationImpl();
                }
            }
        }
        return dxfLineTransformation;
    }

    /**
     * 将几何弧线、转换为点弧线数据
     *
     * @param geometricArcList 需要转换的数据
     * @return 返回点线数据
     */
    @Override
    public List<LWPolyline> arcTransform(List<Arc> geometricArcList) {
        List<LWPolyline> dxfLineList = new ArrayList<>();
        for (Arc geometricArc : geometricArcList) {
            LWPolyline lwPolyline = new LWPolyline();
            lwPolyline.setLayer(geometricArc.getLayer());
            lwPolyline.setColor(geometricArc.getColor());
            lwPolyline.setColorRGB(geometricArc.getColorRGB());
            lwPolyline.setClosed(false);
            lwPolyline.setDocument(geometricArc.getDocument());
            lwPolyline.setID(geometricArc.getID());
            lwPolyline.setLineWeight(geometricArc.getLineWeight());
            lwPolyline.setLinetypeScaleFactor(geometricArc.getLinetypeScaleFactor());
            lwPolyline.setLineType(geometricArc.getLineType());

            Point3D centerPoint = geometricArc.getCenterPoint();

            double startAngle = geometricArc.getStartAngle();
            double endAngle = geometricArc.getEndAngle();
            if (startAngle > endAngle) {
                endAngle = endAngle + 360;
            }
            //编号从第一个点开始
            Long num = 1L;
            //圆心x
            Double x = centerPoint.getX();
            //圆心y
            Double y = centerPoint.getY();
            //半径
            Double r = geometricArc.getRadius();
            // 起点半径
            Double radius = startAngle;

            while (radius < endAngle) {
                Double arc = (Math.PI / 180) * radius;
                Double x1 = x + r * (Math.cos(arc));
                Double y1 = y + r * (Math.sin(arc));
                LW2DVertex point2 = new LW2DVertex(num, x1, y1);
                lwPolyline.addVertex(point2);
                radius = radius + 1;
                num = num + 1L;
            }
            if (radius >= endAngle) {
                radius = radius - 1;
                radius = radius + (endAngle - radius);
                Double arc2 = (Math.PI / 180) * radius;
                Double x2 = x + r * (Math.cos(arc2));
                Double y2 = y + r * (Math.sin(arc2));
                LW2DVertex point3 = new LW2DVertex(num, x2, y2);
                lwPolyline.addVertex(point3);
                num = num + 1L;
            }
            dxfLineList.add(lwPolyline);
        }
        return dxfLineList;
    }


    @Override
    public List<LWPolyline> circleTransform(List<Circle> circles) {
        List<LWPolyline> dxfLineList = new ArrayList<>();

        for (Circle circle : circles) {
            LWPolyline lwPolyline = new LWPolyline();
            lwPolyline.setLayer(circle.getLayer());
            lwPolyline.setColor(circle.getColor());
            lwPolyline.setColorRGB(circle.getColorRGB());
            lwPolyline.setClosed(true); // 闭合的线是圆
//            lwPolyline.setElevation();
            lwPolyline.setDocument(circle.getDocument());
            lwPolyline.setVisibile(true);
            lwPolyline.setID(circle.getID());
            lwPolyline.setLineWeight(circle.getLineWeight());
            lwPolyline.setLinetypeScaleFactor(circle.getLinetypeScaleFactor());
            lwPolyline.setLineType(circle.getLineType());

            Point3D centerPoint = circle.getCenterPoint();

            //圆心x
            Double x = centerPoint.getX();
            //圆心y;
            Double y = centerPoint.getY();
            //圆心z
            Double z = centerPoint.getZ();
            //半径
            Double r = circle.getRadius();

            Double radius = 2.0;
            Double end = 360.0;

            //编号从第二个点开始
            Long num = 2L;

            //起点
            LW2DVertex startPoint = new LW2DVertex(1L, x + r, y);
            lwPolyline.addVertex(startPoint);
            while (radius < end) {
                double arc = (Math.PI / 180) * radius;
                double x1 = x + r * (Math.cos(arc));
                double y1 = y + r * (Math.sin(arc));
                LW2DVertex endPoint = new LW2DVertex(num, x1, y1);
                num = num + 1L;
                lwPolyline.addVertex(endPoint);
                radius = radius + 2.0;
            }
            dxfLineList.add(lwPolyline);
        }
        return dxfLineList;
    }

}
